package com.yuejian_06.yuejian_06.activity;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.yuejian_06.yuejian_06.Listener.LoginListener;
import com.yuejian_06.yuejian_06.R;
import com.yuejian_06.yuejian_06.bean.SuccessBean;
import com.yuejian_06.yuejian_06.model.LoginModel;

import static com.yuejian_06.yuejian_06.R.id.pass;

public class RegisterActivity extends BaseActivity {
    EditText username,password,phonenum,adress;//布局声明
    Button back,reg;

    private LoginListener<SuccessBean> Listener=new LoginListener<SuccessBean>() {
        @Override
        public void onResponse(SuccessBean o) {
            SuccessBean res=o;
            String success=res.getSuccess();
            if ("0".equals(success))
            {
                //处理失败
                Toast.makeText(RegisterActivity.this,"注册失败",Toast.LENGTH_SHORT).show();

            }
            else{
                //成功intent跳转到loginActivity中
                Intent intent= new Intent();
                intent.putExtra("username",phonenum.getText().toString());
                setResult(RESULT_OK,intent);
                Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_LONG).show();
                finish();
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(getApplicationContext(),"失败了",Toast.LENGTH_SHORT).show();

        }
    };

    @Override
    void initViews() {
        //设置布局文件
        setLayout(R.layout.activity_register);
        username = (EditText)findViewById(R.id.reg_name);
        password =(EditText)findViewById(pass);
        phonenum = (EditText)findViewById(R.id.mobilenum);
        adress = (EditText)findViewById(R.id.adress);
        back = (Button)findViewById(R.id.back);
        reg = (Button)findViewById(R.id.reg);
    }

    @Override
    void initEvents() {
        //返回
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        //注册
        reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LoginModel loginModel=new LoginModel();
                loginModel.reg(username.getText().toString(),
                                password.getText().toString(),
                                adress.getText().toString(),
                                phonenum.getText().toString(),Listener);
            }
        });


    }

    @Override
    void initData() {

    }
}
